window.addEventListener('load', function () {

    var focus = document.querySelector('.focus');
    var arrow_l = document.querySelector('.arrow-l');
    var arrow_r = document.querySelector('.arrow-r');

    var focusWidth = focus.offsetWidth;

    focus.addEventListener('mouseover', function () {
        arrow_l.style.display = 'block';
        arrow_r.style.display = 'block';
    })
    focus.addEventListener('mouseout', function () {
        arrow_l.style.display = 'none';
        arrow_r.style.display = 'none';
    })


    var ul = focus.querySelector('ul');
    var ol = document.querySelector('.circle');
    var focusWidth = focus.offsetWidth;
    var num = 0;
    var circle = 0;
    // 节流阀
    var flag = true;

    for (var i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li');

        li.setAttribute('index', i);
        ol.appendChild(li);

        li.addEventListener('click', function () {
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = '';
            }
            this.className = 'current';

            var index = this.getAttribute('index');
            num = index
            circle = index;
            animate(ul, -index * focusWidth);
        })
    }

    ol.children[0].className = 'current';
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);


    arrow_r.addEventListener('click', function () {
        if(flag){
            flag = false;
            if (num == ul.children.length - 1) {
                ul.style.left = 0;
                num = 0;
            }
            num++;
    
            animate(ul, -num * focusWidth,function(){
                flag = true;
            });
            circle++;
            if (circle == ul.children.length - 1) {
                circle = 0;
            }
            circlieChange();
        }
    });


    arrow_l.addEventListener('click', function () {
       if(flag){
           flag = false
        if (num == 0) {
            num = ul.children.length-1;
            ul.style.left = -num * focusWidth + 'px'; 
        }
        num--;
        animate(ul, -num * focusWidth,function(){
            flag = true;
        });
        circle--;
        if (circle < 0) {
            circle = 3;
        }
        circlieChange();
       }
    });

    function circlieChange(){
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'current';
    }

    var timer = setInterval(function(){
        arrow_r.click();
    },2000);


})